package com.entity.sys;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.utils.annotation.NotEmpty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author clay
 * @since 2022-02-15
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("t_user")
@ApiModel(value = "User对象", description = "")
public class User extends BaseEntity implements Serializable, UserDetails {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "姓名")
    private String realName;

    @ApiModelProperty(value = "昵称")
    private String nickname;

    @NotEmpty(message = "用户名必填")
    @ApiModelProperty(value = "登录用户名")
    private String username;

    @NotEmpty(message = "密码必填")
    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "联系方式")
    private String phone;

    @ApiModelProperty(value = "学部id")
    private String departmentId;

    @NotEmpty(message = "工号或学号必填")
    @ApiModelProperty(value = "工号或学号")
    private String nbr;

    @ApiModelProperty(value = "头像")
    private String image;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "最后登录时间")
    private String lastLoginTime;

    @ApiModelProperty(value = "是否启用0 关闭， 1 启用")
    private Boolean enable;

    @ApiModelProperty(value = "性别 男 女")
    private String sex;

    @ApiModelProperty(value = "生日")
    private String age;

    @ApiModelProperty(value = "政治面貌")
    private String politicId;

    @ApiModelProperty(value = "民族")
    private String nationName;

    @ApiModelProperty(value = "籍贯")
    private String nationPlace;

    @ApiModelProperty(value = "邮箱")
    private String email;
    @ApiModelProperty(value = "联系地址")
    private String address;
    @ApiModelProperty(value = "职称等级id")
    private String jobLevelId;
    @ApiModelProperty(value = "职位id")
    private String posId;
    @ApiModelProperty(value = "最高学历;'博士','硕士','本科','大专','高中','初中','小学','其他'")
    private String degree;
    @ApiModelProperty(value = "所学专业")
    private String specialty;
    @ApiModelProperty(value = "毕业院校")
    private String school;
    @ApiModelProperty(value = "入职")
    private String beginDate;
    @ApiModelProperty(value = "离职日期")
    private String endWordDate;
    @ApiModelProperty(value = "在职状态;'在职','离职'")
    private String workState;
    @ApiModelProperty(value = "合同期限")
    private String contractTerm;
    @ApiModelProperty(value = "转正日期")
    private String conversionTime;
    @ApiModelProperty(value = "合同开始时间")
    private String beginContract;
    @ApiModelProperty(value = "合同结束时间")
    private String endContract;
    @ApiModelProperty(value = "工龄")
    private String workAge;
    @ApiModelProperty(value = "工资账套id")
    private String salaryId;
    @ApiModelProperty(value = "角色")
    @TableField(exist = false)
    private List<Role> roles;

    //获取权限
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        System.out.println("权限中的roles" + JSON.toJSONString(roles));
        if (CollectionUtil.isEmpty(roles)) {
            return null;
        }
        List<SimpleGrantedAuthority> authorities = roles.stream().map(role ->
                new SimpleGrantedAuthority(role.getName()))
                .collect(Collectors.toList()
                );
        return authorities;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enable;
    }
}
